package base;

import db.FoodManager;
import db.IPersistable;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.*;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Shayan
 */
public class Category extends Entity implements IPersistable {

    public Category() {
    }

    public Category(String name) {
        this.name = name;
    }

    public Category(String id, String name) {
        setId(id);
        this.name = name;
    }
    
    /**
     * Name
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * getFoods
     */
    public Vector getFoods()
            throws InvalidRecordIDException, RecordStoreNotOpenException, IOException, RecordStoreException {
        return FoodManager.getAllByCategory(this.getId());
    }

    /**
     * validate
     * @throws ValidationException
     */
    public void validate() throws ValidationException {
    }

    public byte[] getData() throws IOException {
        ByteArrayOutputStream bin = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bin);
        out.writeUTF(getId());
        out.writeUTF(name);

        out.flush();
        return bin.toByteArray();
    }

    public static Category create(byte[] data) throws IOException {
        Category category = new Category();
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream in = new DataInputStream(bin);

        category.setId(in.readUTF());
        category.setName(in.readUTF());
        return category;
    }
}
